package com.ios.controller;

import com.ios.dto.CallLogExecution;
import com.ios.enums.CallLogEnum;
import com.ios.service.impl.CallLogServiceImpl;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/call",method = {RequestMethod.POST})
public class CaLLController {
    @Autowired
    private CallLogServiceImpl callLogService;

    @RequestMapping(value = "/addlog")
    @ResponseBody
    public Map<String,Object> CallLog(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int disabler_id = HttpServletRequestUtil.getInt(request,"disabler_id");
        try{
            CallLogExecution cl = callLogService.CreateCall(disabler_id);
            if (cl.getState()== CallLogEnum.CALL_SUCCESS.getState()){
                modelMap.put("success",true);
                modelMap.put("FirstPhone",cl.getPhone());
            }else if (cl.getState()==CallLogEnum.CALL_SUCCESS_RELATIVE.getState()){
                modelMap.put("success",true);
                modelMap.put("RelativePhone",cl.getPhone());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",cl.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }

}
